#!/usr/bin/env python
# encoding: utf-8
# @Time    : 2020/8/24 18:40
# @Author  : Mr. Cui
# @File    : views.py
# @Software: PyCharm
# project蓝图的视图
import json

from flask import request

from . import collect
from .service import weekly_service
from ..collect.service import collect_service, emial_collect, send_testemail, collect_manage, collect_pro, send_report
from ...libs import result, const
from ...libs.validate_params import validate_params


# 看板一级目录
@collect.route('/venus_first_level', methods=['POST'])
def venus_first_level():
    '''
    看板一级目录
    :return:
    '''
    data = json.loads(request.data)
    mgrop = data["mgrop"]
    res = collect_service.venus_first_level(mgrop)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


# 看板二级目录
@collect.route('/venus_two_level/<groupname>', methods=['get'])
def venus_two_level(groupname):
    '''
    看板二级目录
    :param groupname:
    :return:
    '''
    res = collect_service.venus_two_level(groupname)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


# 看板三级目录
@collect.route('/venus_three_level/<venus_pname>', methods=['get'])
def venus_three_level(venus_pname):
    '''
    看板三级目录
    :param venus_pname:
    :return:
    '''
    res = collect_service.venus_three_level(venus_pname)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


# mantis一级目录
@collect.route('/mantis_first_level', methods=['POST'])
def mantis_first_level():
    '''
    mantis一级目录
    :return:
    '''
    data = json.loads(request.data)
    vgoup = data["vgoup"]
    res = collect_service.mantis_first_level(vgoup)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


# mantis看板二级目录
@collect.route('/mantis_two_level/<group_name>', methods=['get'])
def mantis_two_level(group_name):
    res = collect_service.mantis_two_level(group_name)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


# mantis看板三级目录
@collect.route('/mantis_three_level/<mantis_pname>', methods=['get'])
def mantis_three_level(mantis_pname):
    res = collect_service.mantis_three_level(mantis_pname)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


# 添加返回查询数据
@collect.route('/get_project_text', methods=['POST'])
# @validate_params(params=["v_groupname", "v_projectname","v_version","m_groupname","m_projectname","m_version"])
def get_project_text():
    data = request.json

    v_groupname = data["v_group"]
    v_projectname = data["v_project"]
    v_version = data["v_version"]
    m_groupname = data["m_group"]
    m_projectname = data["m_project"]
    m_version = data["m_version"]
    res = collect_service.get_project(v_groupname, v_projectname, v_version, m_projectname, m_version, )
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


@collect.route('/get_collect_list', methods=['post'])
# @validate_params(
#     params=["page_num", "page_per", "group_name", "project_name", "status", "project_str", "start_time", "end_time",
#             "demand_type"])
def get_collect_list():
    """
    质量收集列表查询
    :return:
    """
    # print(request.json)
    res = collect_service.get_collect_list(request.json)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


@collect.route('/get_project/<project_id>', methods=['GET'])
def get_project(project_id):
    """
    项目详情
    :return:
    """
    res = collect_service.get_collect(project_id)
    return result(const.SUCCESS, const.QUERY_SUCCESS, res)


@collect.route('/delete_collect/<pid>', methods=['GET'])
def delete_collect(pid):
    """
    质量收集-删除
    :return:
    """
    res = collect_service.delete_collect(pid)
    return result(const.SUCCESS, res, None)


@collect.route('/get_replytest/<pid>', methods=['get'])
def get_replytest(pid):
    """
    回显提测邮件数据
    """
    res = emial_collect.get_test_data(pid)
    return result(const.SUCCESS, None, res)


@collect.route('/update_testdata', methods=['post'])
@validate_params(params=["id", "addressee", "cc", "project", "pversion", "platform", "mantis_adress"])
def update_testdata():
    """
    修改/插入提测邮件数据
    """
    res = emial_collect.edit_testdata(request.json)
    return result(const.SUCCESS, res, None)


@collect.route('/get_reportdata/<pid>', methods=['get'])
def get_reportdata(pid):
    """
    回显测试报告邮件数据
    """
    res = emial_collect.get_report_data(pid)
    return result(const.SUCCESS, None, res)


@collect.route('/update_report_data', methods=['post'])
@validate_params(params=["id", "addressee", "cc", "project_name", "begin_time", "end_time", "several_days",
                         "pro_manager", "developer", "qa", "bug_counts", "bug_repair", "bug_legacy",
                         "advice_counts", "bug_density", "bug_introd", "conclusion", "mantis_adress",
                         "mantis_account", "remarks"])
def update_report_data():
    """
    修改/插入测试报告邮件数据
    """
    msg = emial_collect.edit_report_data(request.json)
    return result(const.SUCCESS, msg, None)


@collect.route('/check_report_emaildata', methods=['get'])
def reply_testemail():
    """
    回复/发送提测邮件
    """
    res = emial_collect.get_data_standand()
    return result(const.SUCCESS, None, res)


@collect.route('send_test_email', methods=['post'])
def send_test_email():
    """
    一键发送提测邮件
    """
    msg = send_testemail.send_reply_test(request.json)
    return result(const.SUCCESS, msg, None)


@collect.route('send_report_email', methods=['post'])
def send_report_email():
    """
    一键发送测试报告
    """
    msg = send_report.send_report_email(request.json)
    return result(const.SUCCESS, msg, None)


# 添加
@collect.route('/add_save', methods=['POST'])
def add_save():
    '''
    添加
    :return:
    '''
    data = request.json
    msg = collect_service.get_save(data)

    return result(const.SUCCESS, msg, None)


@collect.route('/get_collect_data/<id>', methods=['get'])
def get_collect_data(id):
    res = emial_collect.get_collect_data(id)
    return result(const.SUCCESS, None, res)


@collect.route('/add_update', methods=['POST'])
def add_update():
    '''
    刷新
    :return:
    '''
    data = request.json
    pid = data["pid"]
    msg = collect_service.get_update(pid)
    return result(const.SUCCESS, msg, None)


@collect.route('/collect_sum', methods=['post'])
def collect_sum():
    """
    项目的工时、参与项目数、准入/出率、按时准入/出率、发布准时率、发布成功率、需求变更次数
    :return:
    """
    data = request.json
    ret = collect_manage.collect_sum_real_time(data)
    return result(const.SUCCESS, None, ret)


@collect.route('/collect_pro', methods=['post'])
def get_collect_pro():
    """
    项目的工时、参与项目数、准入/出率、按时准入/出率、发布准时率、发布成功率、需求变更次数
    :return:
    """
    data = request.json
    ret = collect_pro.get_collect_pro(data)
    return result(const.SUCCESS, None, ret)


@collect.route('/get_all_info', methods=['post'])
def get_all_info():
    """
    获取版本对应关系
    :return:
    """
    data = request.json
    ret = collect_service.get_project_all_info(data)
    return result(const.SUCCESS, None, ret)


@collect.route('/get_collect_question', methods=['post'])
@validate_params(params=["radio", "page_num", "page_per"])
def get_collect_question():
    """
    获取业务线问题
    参数：业务线、时间
    :return:
    """
    data = request.json
    ret = collect_pro.get_collect_question(data)
    return result(const.SUCCESS, None, ret)


@collect.route('/verification_collect_version', methods=['post'])
def verification_collect_version():
    """
    判断是否已经关联过
    :return:
    """
    data = request.json
    ret = collect_service.verification_collect_version(data)
    return result(const.SUCCESS, None, ret)


@collect.route('/get_weekly_time', methods=['get'])
def get_weekly_time():
    """
    获取collect_sum表周时间
    :return:
    """
    ret = weekly_service.get_weekly_time()
    return result(const.SUCCESS, None, ret)


@collect.route('/get_weekly_bug_rate', methods=['post'])
def get_weekly_bug_rate():
    """
    获取近三周bug率
    :return:
    """
    ret = weekly_service.get_weekly_bug_rate(request.json)
    return result(const.SUCCESS, None, ret)


@collect.route('/get_weekly_collect', methods=['post'])
def get_weekly_collect():
    """
    获取相应时间内的项目指标收集列表
    时间范围必传
    :return:
    """
    ret = weekly_service.get_weekly_collect(request.json)
    return result(const.SUCCESS, None, ret)


@collect.route('/add_weekly', methods=['post'])
def add_weekly():
    """
    新建周报
    :return:
    """
    msg = weekly_service.add_weekly(request.json)
    return result(const.SUCCESS, msg, None)


@collect.route('/get_weekly_list', methods=['post'])
@validate_params(params=["page_num", "page_per"])
def get_weekly_list():
    """
    周报列表查询
    :return:
    """
    ret = weekly_service.get_weekly_list(request.json)
    return result(const.SUCCESS, None, ret)


@collect.route('/get_weekly/<id>', methods=['get'])
def get_weekly(id):
    """
    获取周报详情
    :return:
    """
    res = weekly_service.get_weekly(id)
    return result(const.SUCCESS, None, res)


@collect.route('/delete_weekly/<id>', methods=['get'])
def delete_weekly(id):
    """
    周报--删除
    :return:
    """
    res = weekly_service.delete_weekly(id)
    return result(const.SUCCESS, res, None)


@collect.route('/personnel_rate/<group>', methods=['get'])
def personnel_rate(group):
    """
    项目的工时、参与项目数、准入/出率、按时准入/出率、发布准时率、发布成功率、需求变更次数
    :return:
    """
    ret = collect_manage.personnel_rate(group)
    return result(const.SUCCESS, None, ret)


@collect.route('/personnel_rate_group_list', methods=['get'])
def personnel_rate_group_list():
    """
    项目的工时、参与项目数、准入/出率、按时准入/出率、发布准时率、发布成功率、需求变更次数
    :return:
    """
    ret = collect_manage.personnel_rate_group_list()
    return result(const.SUCCESS, None, ret)


@collect.route('/collect_trend', methods=['get'])
def collect_trend():
    """
    质量趋势
    :return: list
    """
    ret = collect_manage.collect_trend()
    return result(const.SUCCESS, None, ret)